﻿CREATE PROCEDURE [dbo].[RolePermission_InsertUpdate]

	@roleId				BIGINT,

	@permissionId		BIGINT,

	@granted			BIT,

	@denied				BIT,

	@modifiedId			VARCHAR (0060)

AS

	BEGIN 

		MERGE RolePermission

			USING (SELECT @roleId, @permissionId) AS Source (RoleId, PermissionId)

				ON ((RolePermission.RoleId = Source.RoleId) AND (RolePermission.PermissionId = Source.PermissionId))

			WHEN MATCHED THEN 

				UPDATE SET 

					Granted = @granted,

					Denied = @denied,

					ModifiedId = @modifiedId,

					ModifiedDateTime = GETDATE ()

			WHEN NOT MATCHED THEN 

				INSERT (RoleId, PermissionId, Granted, Denied, CreateId, CreateDateTime, ModifiedId, ModifiedDateTime) 

					VALUES (@roleId, @permissionId, @granted, @denied, @modifiedId, GETDATE (), @modifiedId, GETDATE())

		; -- MERGE ( END )


	END

	